split and join¶

🎨 split¶

In [2]:
'Here are some words'.split()
Out[2]:
['Here', 'are', 'some', 'words']
In [3]:
'blahblahblah'.split()
Out[3]:
['blahblahblah']
In [4]:
for word in 'here are some words, and there might be many'.split():
    print(word)
here
are
some
words,
and
there
might
be
many
In [6]:
"cat" in "Can you concatenate two strings?".split()
# str in list[str] -> # is the word in the list of words?
# str in str -> # is the substring found in the bigger string?
Out[6]:
False
In [7]:
print('Do  you     know  \n      the     \t          muffin      \n              man?'.split())
['Do', 'you', 'know', 'the', 'muffin', 'man?']

cats.py 🐈 🐈‍⬛¶

🎨 join¶

In [14]:
' '.join(['red', 'fish', 'blue', 'fish'])
Out[14]:
'red fish blue fish'
In [9]:
" :) ".join(['red', 'fish', 'blue', 'fish'])
Out[9]:
'red :) fish :) blue :) fish'
In [10]:
seuss = ['red', 'fish', 'blue', 'fish']
fish = ' 🐟 '
fish.join(seuss)
Out[10]:
'red 🐟 fish 🐟 blue 🐟 fish'

cougars.py¶

🖌 CSV - Comma Separated Values¶

In [27]:
'Name,Favorite place,School,Department'.split(',')
Out[27]:
['Name', 'Favorite place', 'School', 'Department']
In [28]:
words = 'Gordon,BYU,BYU,Computer Science'.split(',')
print(words)
print('-'.join(words))
['Gordon', 'BYU', 'BYU', 'Computer Science']
Gordon-BYU-BYU-Computer Science

👨🏻‍🎨 unemployment.py¶

The file unemployment.txt looks like this:

          State/Area  Year  Month  %_Unemployed
             Alabama  1976      1           6.6
              Alaska  1976      1           7.1
             Arizona  1976      1          10.2
            Arkansas  1976      1           7.3
          California  1976      1           9.2
  Los-Angeles-County  1976      1           8.9

Write a program that takes a file like unemployment.txt and a state and prints the maximum unemployment observed for that state.

What are the different list patterns you might use?

The file unemployment.txt looks like this:

          State/Area  Year  Month  %_Unemployed
             Alabama  1976      1           6.6
              Alaska  1976      1           7.1
             Arizona  1976      1          10.2
            Arkansas  1976      1           7.3
          California  1976      1           9.2
  Los-Angeles-County  1976      1           8.9

Write a program that takes a file like unemployment.txt and a year and prints the average unemployment observed for that year.

What are the different list patterns you might use?

What other questions can YOU answer with this data?

Key Ideas¶

  • split
    • default: all whitespace
    • or give it a substring to split on
  • join
  • Tabular data
    • Each line is a list of elements